------------------------------------------------------------------------------
-- Mining script for computercraft's turtles by Hea3veN
-- Version 0.1
------------------------------------------------------------------------------
loadfile("lib/status.lua")()
loadfile("lib/state_machine.lua")()
loadfile("lib/turtle_utils.lua")()
loadfile("lib/inventory.lua")()

function d()
	return statusGetProgData()
end

function atOddLevel()
	return (d().y % 2) == 1
end

progStates = {
	init=function()
		d().direction='right'
		d().x = 1
		d().z = 1
		d().y = 1
		statusSave()
		turtle.turnRight()
		return 'goDown'
	end,
	forward=function()
		local step = 0
		if d().direction == 'right' then
			if d().x >= d().sizeX then
				return 'turnStep1'
			end
			step = 1
		else
			if d().x <= 1 then
				return 'turnStep1'
			end
			step = -1
		end
		if not forward(1) then
			return 'end'
		end
		d().x = d().x + step
		
		if inventoryIsFull() then
			if d().y == d().sizeY then
				d().nextState = 'goUp'
			else
				d().nextState = 'goDown'
			end
			return 'dropInventory'
		end
		if d().y == d().sizeY then
			return 'goUp'
		else
			return 'goDown'
		end
	end,
	turnStep1=function()
		if d().direction == 'right' then
			turtle.turnLeft()
		else
			turtle.turnRight()
		end
		return 'turnStep2'
	end,
	turnStep2=function()
		if d().z >= d().sizeZ then
			return 'end'
		end
		
		if not forward(1) then
			return 'end'
		end
		d().z = d().z + 1
		return 'turnStep3'
	end,
	turnStep3=function()
		if d().direction == 'right' then
			turtle.turnLeft()
		else
			turtle.turnRight()
		end
		if d().direction == 'right' then
			d().direction = 'left'
		else
			d().direction = 'right'
		end
		if d().y == d().sizeY then
			return 'goUp'
		else
			return 'goDown'
		end
	end,
	--
	goDown=function()
		print("goDown")
		if d().y == d().sizeY then
			return 'forward'
		end
		print("Going down")
		if not down(1) then
			return 'end'
		end
		d().y = d().y + 1
		
		if inventoryIsFull() then
			d().nextState = 'goDown'
			return 'dropInventory'
		end
		return 'goDown'
	end,
	goUp=function()
		if d().y == 1 then
			return 'forward'
		end
		if not up(1) then
			return 'end'
		end
		d().y = d().y - 1
		
		if inventoryIsFull() then
			d().nextState = 'goUp'
			return 'dropInventory'
		end
		return 'goUp'
	end,
	dropInventory=inventoryDropState
}

arg = { ... }

if (arg[1] == nil) then
	if not statusLoad() then
		print("Usage: clear x z y\n\tClear an area of x width, z depth and y height")
	else
		if not statusLoad() then
			print("Usage: clear x z y\n\tClear an area of x width, z depth and y height")
		end
		smStart(progStates)
	end
elseif (arg[2] == nil) or (arg[3] == nil) then
	print("Usage: clear x z y\n\tClear an area of x width, z depth and y height")
else

	sizex = tonumber(arg[1])
	sizez = tonumber(arg[2])
	sizey = tonumber(arg[3])

	if not statusLoad() then
		statusInit('mine2')
	end
	
	d().sizeX = sizex
	d().sizeZ = sizez
	d().sizeY = sizey
	statusSave()

	smStart(progStates)
end